using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PoliceBase;

namespace CarControl
{
    public partial class FormMain : Form
    {
        #region DataFields
        private DBBasic dd;
        private int carID;
        private int incidentID = -1;
        private Address targetAddress = null;
        #endregion

        #region Constructors
        public FormMain()
        {
            InitializeComponent();

            carID = Properties.Settings.Default.CarID;
            dd = new DBBasic(Properties.Settings.Default.Conn);

            mapControl1.ConnectionString = Properties.Settings.Default.Conn;
            mapControl1.DisplayAllCars = false;
            mapControl1.CarID = this.carID;
        }
        #endregion

        #region EventHandlers
        private void timer1_Tick(object sender, EventArgs e)
        {
            dd.UpdateCarPosition(carID, GPS.GetPosition(), GPS.GetPathID());
            mapControl1.Invalidate();
        }
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = (MessageBox.Show(this, "Are you sure you want to exit?", "Car Controller", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No);
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            incidentID = dd.GetIncID(this.carID);
            if (incidentID != -1)
            {
                targetAddress = dd.LoadIncident(incidentID).Address;
                lblIncID.Text = "IncidentID : " + incidentID.ToString();
                lblAddress.Text = "Target Address : " + targetAddress.ToString();

                lblIncID.Visible = true;
                lblAddress.Visible = true;
                buttonCovered.Visible = true;

                timer2.Enabled = false;

                mapControl1.High = dd.GetShortestPath(GPS.GetPosition(), GPS.GetPathID(), incidentID);
                MessageBox.Show(this, "New Target \n" + targetAddress.ToString(), "Car Controller", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonCovered_Click(object sender, EventArgs e)
        {
            dd.SetIncidentCovered(incidentID);
            dd.ClearTarget(this.carID);

            mapControl1.High = null;
            mapControl1.Invalidate();
            
            timer2.Enabled = true;
            buttonCovered.Visible = false;
            lblIncID.Visible = false;
            lblAddress.Visible = false;

            incidentID = -1;
        }
        #endregion
    }
}